package logc;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

import logc.model.Comment;
import logc.repository.session.CommentSessionRepository;
import logc.util.Const;
import logc.util.MailUtil;
//

//LogCollectorJAVA 메인

public class LogCollectorMain {
	void Rinfo() throws IOException{
				
		String filepath = null; //파일경로
		String emailaddress = null; //이메일 주소
		String code = null; //코드
		
		//String dilimeter ="";
				
		//config 파일 내용 불러오기
		Const cnt = new Const();
		
		//dilimeter = cnt._DELIMITER;
		filepath = cnt._FILEPATH;
		emailaddress = cnt._EMAILADDRESS;
		code = cnt._CODE;
			
		BufferedReader in = null;
		
		try{
			
			in = new BufferedReader(new FileReader(filepath));
			
			String s;
			String findStr = code + ".*";
			String emailmsg = ""; //이메일 본문
			
			while((s = in.readLine()) != null){
			
				InsertExe(s);
				
				if (s.matches(findStr)){
					
					emailmsg += s + "<br>\n";
				}		
			}
		
			System.out.println(emailmsg);
			
			 //담당자에게 메일 발송
			if(emailmsg != ""){ //코드 값 체크
				//코드값 있을시 담당자에게 이메일 보내기
				
				StringBuffer msg = new StringBuffer();
				
				/*msg.append("오류 안내 메일입니다." + "\n\n");
				msg.append("Log 오류 내용 : \n\n" + emailmsg +"\n\n");
				msg.append("신속한 조치 바랍니다." + "\n\n");*/
				msg.append("<HTML>\n");
				msg.append("<HEAD>\n");
				msg.append("<TITLE></TITLE>\n");
				msg.append("</HEAD>\n");
				msg.append("<BODY>\n");
				msg.append("<TABLE border=1>\n");
				msg.append("<TR>\n");
				msg.append("<TD>제목\n");
				msg.append("</TD>\n");
				msg.append("<TD>오류 안내 메일입니다\n");
				msg.append("</TD>\n");
				msg.append("</TR>\n");
				msg.append("<TR>\n");
				msg.append("<TD>Log 오류 내용\n");
				msg.append("</TD>");
				msg.append("<TD>"+emailmsg+"</TD>\n");
				msg.append("</TR>\n");
				msg.append("<TR>\n");
				msg.append("<TD>안내\n");
				msg.append("</TD>\n");
				msg.append("<TD>신속한 조치 바랍니다\n");
				msg.append("</TD>\n");
				msg.append("</TR>\n");
				msg.append("</TABLE>\n");
				msg.append("</BODY>\n");
				msg.append("</HTML>");

				Thread thread = new Thread(new MailUtil("[System] 로그 알람 메일입니다.", msg.toString() , emailaddress));
				thread.start();
			}
			 
		}catch(IOException e){
			
			System.err.println(e);
			System.exit(1);
		}finally{
			if(in != null){
				in.close();
			}
		}
	}
	
	//private final CommentSessionRepository commentSessionRepository = new CommentSessionRepository();
	
	public void InsertExe(String vals) { 

		String code ="";
		String datetime = "";
		String grade = "";
		String codename ="";
		String logdetail ="";

			
		StringTokenizer st = new StringTokenizer(vals, "|");
		
		while(st.hasMoreTokens()){
			code = st.nextToken();
			datetime = st.nextToken();
			grade = st.nextToken();
			codename = st.nextToken();
			logdetail = st.nextToken();
		}

		
		Comment comment = new Comment();
		comment.setCode(code);
		comment.setDatetime(datetime);
		comment.setGrade(grade);
		comment.setCodename(codename);
		comment.setLogdetail(logdetail);

		Integer result = CommentSessionRepository.insertLogmast(comment);

		System.out.println("result = " + result);
	}
	
public static void main(String args[]) throws IOException{
	
	LogCollectorMain lcm = new LogCollectorMain();
	lcm.Rinfo();
	
	}
}